//
//  UIViewController+MonitoringNetwork.m
//  template_test
//
//  Created by stone on 16/9/5.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "AFNetworkReachabilityManager.h"
#import "UIViewController+MonitoringNetwork.h"
#import "ConstValues.h"
@import Darwin.POSIX.netinet.in;

@implementation NSObject (SNExtentsion)

+ (void)exchangeInstanceMethod1:(SEL)method1 method2:(SEL)method2 {
    method_exchangeImplementations(class_getInstanceMethod(self, method1), class_getInstanceMethod(self, method2));
}

+ (void)exchangeClassMethod1:(SEL)method1 method2:(SEL)method2 {
    method_exchangeImplementations(class_getClassMethod(self, method1), class_getClassMethod(self, method2));
}
- (void)expendTime:(void (^)())block {
    double start = CFAbsoluteTimeGetCurrent();
    block();
    NSLog(@"时间差 %f", CFAbsoluteTimeGetCurrent() - start);
}

@end
//sn_note:========= UIViewController ============= stone 🐳 ===========/

@implementation UIViewController (MonitoringNetwork)
+ (void)load {

    [self exchangeInstanceMethod1:NSSelectorFromString(@"viewDidLoad") method2:NSSelectorFromString(@"sn_viewDidLoad")];
}
- (BOOL)reachable {
    return [AFNetworkReachabilityManager sharedManager].reachable;
}
//- (instancetype)sn_init {
//    NSLog(@"进入分类");
//    [self sn_init];
//    [self sn_addObserver];
//
//    return self;
//}
- (void)sn_viewDidLoad {

    [self sn_viewDidLoad];

    [self sn_addObserver];
}
- (void)sn_addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedWiFiNotification:) name:WiFiNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedWWANNotification:) name:WWANNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotReachableNotification:) name:NotReachableNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedUnknownNotification:) name:UnknownNotification object:nil];
}

- (void)receivedWiFiNotification:(NSNotification *)notify {
    NSLog(@"%@", @"is wifi");
}
- (void)receivedWWANNotification:(NSNotification *)notify {
    NSLog(@"%@", @"is 4G or 3G");
}
- (void)receivedNotReachableNotification:(NSNotification *)notify {
    NSLog(@"%@", @"is not network");
    
   UIView * view =  [[UIApplication sharedApplication].delegate.window viewWithTag:444];
    
    if (view) {
//        [view removeFromSuperview];
//        MBProgressHUD * hud = [SNProgressManager showToView:[UIApplication sharedApplication].delegate.window message:@"" afterDelay:2];
//        hud.tag = 444;
    }else{
        MBProgressHUD * hud = [SNProgressManager showToView:[UIApplication sharedApplication].delegate.window message:@"网络异常" afterDelay:2];
        hud.tag = 444;
    }
}
- (void)receivedUnknownNotification:(NSNotification *)notify {
    NSLog(@"%@", @"is unknow network");
}

- (void)sn_dealloc {

//    /** 先消除我的 , 再消除用户的, 因为用户的是系统级别的 */
//    NSLog(@"im dead");
//    /** 判断观察者是否存在, 如果不存在 打印 null */
//    NSLog(@"self.observationInfo = %@", self.observationInfo);
//    NSString * observationInfoString = [NSString stringWithFormat:@"%@", self.observationInfo];
//    NSLog(@"%@", [observationInfoString containsString:@"sn_rowCount"] ? @"yes" : @"no");
//    //  self.observationInfo
//
//    if ([observationInfoString containsString:@"sn_rowCount"]) {
//
//        NSLog(@"sn_rowCount have observationInfo %@", [observationInfoString containsString:@"sn_rowCount"] ? @"yes" : @"no");
//        [self removeObserver:self.manager forKeyPath:@"sn_rowCount" context:&sn_rowCountContext];
//    }
//    if ([observationInfoString containsString:@"sn_dataSource"]) {
//        NSLog(@"sn_dataSource have observationInfo %@", [observationInfoString containsString:@"sn_rowCount"] ? @"yes" : @"no");
//        [self removeObserver:self.manager forKeyPath:@"sn_dataSource" context:&sn_dataSourceContext];
//    }
    [self sn_dealloc];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
//
}


@end

